package com.jiuling.flyweight;

import java.util.HashMap;
import java.util.Map;

abstract class Monster {
    protected String appearance;
    protected int health;
    protected int attack;

    public void display() {
        System.out.println("Appearance: " + appearance);
        System.out.println("Health: " + health);
        System.out.println("Attack: " + attack);
    }

    public abstract void attack();
}

class GiantMonster extends Monster {
    public GiantMonster() {
        appearance = "Giant";
        health = 100;
        attack = 50;
    }

    @Override
    public void attack() {
        System.out.println("Giant monster is attacking!");
    }
}

class SkeletonMonster extends Monster {
    public SkeletonMonster() {
        appearance = "Skeleton";
        health = 50;
        attack = 10;
    }

    @Override
    public void attack() {
        System.out.println("Skeleton monster is attacking!");
    }
}

class SlimeMonster extends Monster {
    public SlimeMonster() {
        appearance = "Slime";
        health = 20;
        attack = 20;
    }

    @Override
    public void attack() {
        System.out.println("Slime monster is attacking!");
    }
}
class MonsterFactory {
    private Map<String, Monster> monsterPool;

    public MonsterFactory() {
        monsterPool = new HashMap<>();
    }

    public Monster getMonster(String type) {
        Monster monster = monsterPool.get(type);
        if (monster == null) {
            switch (type) {
                case "giant":
                    monster = new GiantMonster();
                    break;
                case "skeleton":
                    monster = new SkeletonMonster();
                    break;
                case "slime":
                    monster = new SlimeMonster();
                    break;
                default:
                    throw new IllegalArgumentException("Invalid monster type!");
            }

            monsterPool.put(type, monster);
        }
        return monster;
    }
}


public class Game {
	public static void main(String[] args) {
        MonsterFactory monsterFactory = new MonsterFactory();

        Monster giant1 = monsterFactory.getMonster("giant");
        giant1.display();
        Monster skeleton = monsterFactory.getMonster("skeleton");
        skeleton.display();
        Monster slime1 = monsterFactory.getMonster("slime");
        slime1.display();
        Monster giant2 = monsterFactory.getMonster("giant");
        giant2.display();
    }

}
